package top.byfree.jbyan.saber.bean.dto

import cn.dev33.satoken.secure.SaSecureUtil
import jakarta.validation.constraints.Email
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Pattern
import jakarta.validation.constraints.Size
import top.byfree.jbyan.saber.bean.IS_DELETE_NO
import top.byfree.jbyan.saber.bean.SECRET_KEY
import top.byfree.jbyan.saber.bean.pojo.User
import java.time.LocalDateTime

/**
 * 注册请求实体
 *
 * @author jbyan
 * @since 21/3/2024
 */
data class RegisterDto(
    /* 用户名称 */
    @field:NotBlank(message = "用户名称不能为空")
    @field:Size(min = 4, max = 12, message = "用户名称需要在4-12位之间")
    @field:Pattern(regexp = "^[a-zA-Z0-9]+\$", message = "用户名称只能包含字母或数字")
    val username: String,
    /* 用户昵称 */
    @field:NotBlank(message = "用户昵称不能为空")
    @field:Size(min = 2, max = 8, message = "用户昵称需要在2-8位之间")
    val nickname: String,
    /* 用户密码 */
    @field:NotBlank(message = "密码不能为空")
    @field:Size(min = 6, max = 12, message = "用户密码需要在6-12位之间")
    @field:Pattern(
        regexp = "^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,}\$",
        message = "用户密码需要包含字母、数字和特殊字符"
    )
    val password: String,
    /* 用户邮箱 */
    @field:NotBlank(message = "邮箱不能为空")
    @field:Email(message = "邮箱格式错误")
    val email: String,
    /* 验证码 */
    @field:NotBlank(message = "验证码不能为空")
    @field:Pattern(regexp = "^\\d{4}\$", message = "验证码格式不正确")
    val code: String
) {
    fun toUser(cUserId: String): User =
        User {
            userId = cUserId
            username = this@RegisterDto.username
            nickname = this@RegisterDto.nickname
            password = SaSecureUtil.aesEncrypt(SECRET_KEY, this@RegisterDto.password)
            email = this@RegisterDto.email
            status = 0
            isDelete = IS_DELETE_NO
            gmtCreate = LocalDateTime.now()
            gmtModify = LocalDateTime.now()
            createBy = this@RegisterDto.username
            modifyBy = this@RegisterDto.username
        }

}